import { VideoType } from './consts'

type VideoTypeMap = (typeof VideoType)[keyof typeof VideoType] | 'auto'

// 检查视频类型
export const checkVideoType = (src: string, type?: VideoTypeMap) => {
	// 优先使用指定的类型
	if (type && type !== 'auto') {
		return type
	}

	// 根据文件后缀名判断类型
	if (/\.m3u8(#|\?|$)/i.exec(src)) {
		return VideoType.HLS
	} else if (/\.flv(#|\?|$)/i.exec(src)) {
		return VideoType.FLV
	} else if (/\.mpd(#|\?|$)/i.exec(src)) {
		return VideoType.DASH
	} else if (/\.mp4(#|\?|$)/i.exec(src)) {
		return VideoType.MP4
	} else if (/\.webm(#|\?|$)/i.exec(src)) {
		return VideoType.WEBM
	} else if (/\.ogg(#|\?|$)/i.exec(src) || /\.ogv(#|\?|$)/i.exec(src)) {
		return VideoType.OGG
	} else {
		return VideoType.UNKNOWN
	}
}
